<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>requeststats.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_labs_net_webchannel_requeststats.js.html">requeststats.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2013 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Static utilities for collecting stats associated with
<a name="line17"></a> * ChannelRequest.
<a name="line18"></a> *
<a name="line19"></a> * @visibility {:internal}
<a name="line20"></a> */
<a name="line21"></a>
<a name="line22"></a>
<a name="line23"></a>goog.provide(&#39;goog.labs.net.webChannel.requestStats&#39;);
<a name="line24"></a>goog.provide(&#39;goog.labs.net.webChannel.requestStats.Event&#39;);
<a name="line25"></a>goog.provide(&#39;goog.labs.net.webChannel.requestStats.ServerReachability&#39;);
<a name="line26"></a>goog.provide(&#39;goog.labs.net.webChannel.requestStats.ServerReachabilityEvent&#39;);
<a name="line27"></a>goog.provide(&#39;goog.labs.net.webChannel.requestStats.Stat&#39;);
<a name="line28"></a>goog.provide(&#39;goog.labs.net.webChannel.requestStats.StatEvent&#39;);
<a name="line29"></a>goog.provide(&#39;goog.labs.net.webChannel.requestStats.TimingEvent&#39;);
<a name="line30"></a>
<a name="line31"></a>goog.require(&#39;goog.events.Event&#39;);
<a name="line32"></a>goog.require(&#39;goog.events.EventTarget&#39;);
<a name="line33"></a>
<a name="line34"></a>
<a name="line35"></a>goog.scope(function() {
<a name="line36"></a>var requestStats = goog.labs.net.webChannel.requestStats;
<a name="line37"></a>
<a name="line38"></a>
<a name="line39"></a>/**
<a name="line40"></a> * Events fired.
<a name="line41"></a> * @type {Object}
<a name="line42"></a> */
<a name="line43"></a>requestStats.Event = {};
<a name="line44"></a>
<a name="line45"></a>
<a name="line46"></a>/**
<a name="line47"></a> * Singleton event target for firing stat events
<a name="line48"></a> * @type {goog.events.EventTarget}
<a name="line49"></a> * @private
<a name="line50"></a> */
<a name="line51"></a>requestStats.statEventTarget_ = new goog.events.EventTarget();
<a name="line52"></a>
<a name="line53"></a>
<a name="line54"></a>/**
<a name="line55"></a> * The type of event that occurs every time some information about how reachable
<a name="line56"></a> * the server is is discovered.
<a name="line57"></a> */
<a name="line58"></a>requestStats.Event.SERVER_REACHABILITY_EVENT = &#39;serverreachability&#39;;
<a name="line59"></a>
<a name="line60"></a>
<a name="line61"></a>/**
<a name="line62"></a> * Types of events which reveal information about the reachability of the
<a name="line63"></a> * server.
<a name="line64"></a> * @enum {number}
<a name="line65"></a> */
<a name="line66"></a>requestStats.ServerReachability = {
<a name="line67"></a>  REQUEST_MADE: 1,
<a name="line68"></a>  REQUEST_SUCCEEDED: 2,
<a name="line69"></a>  REQUEST_FAILED: 3,
<a name="line70"></a>  BACK_CHANNEL_ACTIVITY: 4
<a name="line71"></a>};
<a name="line72"></a>
<a name="line73"></a>
<a name="line74"></a>
<a name="line75"></a>/**
<a name="line76"></a> * Event class for SERVER_REACHABILITY_EVENT.
<a name="line77"></a> *
<a name="line78"></a> * @param {goog.events.EventTarget} target The stat event target for
<a name="line79"></a>       the channel.
<a name="line80"></a> * @param {requestStats.ServerReachability} reachabilityType
<a name="line81"></a> *     The reachability event type.
<a name="line82"></a> * @constructor
<a name="line83"></a> * @extends {goog.events.Event}
<a name="line84"></a> */
<a name="line85"></a>requestStats.ServerReachabilityEvent = function(target, reachabilityType) {
<a name="line86"></a>  goog.events.Event.call(this,
<a name="line87"></a>      requestStats.Event.SERVER_REACHABILITY_EVENT, target);
<a name="line88"></a>
<a name="line89"></a>  /**
<a name="line90"></a>   * @type {requestStats.ServerReachability}
<a name="line91"></a>   */
<a name="line92"></a>  this.reachabilityType = reachabilityType;
<a name="line93"></a>};
<a name="line94"></a>goog.inherits(requestStats.ServerReachabilityEvent, goog.events.Event);
<a name="line95"></a>
<a name="line96"></a>
<a name="line97"></a>/**
<a name="line98"></a> * Notify the channel that a particular fine grained network event has occurred.
<a name="line99"></a> * Should be considered package-private.
<a name="line100"></a> * @param {requestStats.ServerReachability} reachabilityType
<a name="line101"></a> *     The reachability event type.
<a name="line102"></a> */
<a name="line103"></a>requestStats.notifyServerReachabilityEvent = function(reachabilityType) {
<a name="line104"></a>  var target = requestStats.statEventTarget_;
<a name="line105"></a>  target.dispatchEvent(
<a name="line106"></a>      new requestStats.ServerReachabilityEvent(target, reachabilityType));
<a name="line107"></a>};
<a name="line108"></a>
<a name="line109"></a>
<a name="line110"></a>/**
<a name="line111"></a> * Stat Event that fires when things of interest happen that may be useful for
<a name="line112"></a> * applications to know about for stats or debugging purposes.
<a name="line113"></a> */
<a name="line114"></a>requestStats.Event.STAT_EVENT = &#39;statevent&#39;;
<a name="line115"></a>
<a name="line116"></a>
<a name="line117"></a>/**
<a name="line118"></a> * Enum that identifies events for statistics that are interesting to track.
<a name="line119"></a> * @enum {number}
<a name="line120"></a> */
<a name="line121"></a>requestStats.Stat = {
<a name="line122"></a>  /** Event indicating a new connection attempt. */
<a name="line123"></a>  CONNECT_ATTEMPT: 0,
<a name="line124"></a>
<a name="line125"></a>  /** Event indicating a connection error due to a general network problem. */
<a name="line126"></a>  ERROR_NETWORK: 1,
<a name="line127"></a>
<a name="line128"></a>  /**
<a name="line129"></a>   * Event indicating a connection error that isn&#39;t due to a general network
<a name="line130"></a>   * problem.
<a name="line131"></a>   */
<a name="line132"></a>  ERROR_OTHER: 2,
<a name="line133"></a>
<a name="line134"></a>  /** Event indicating the start of test stage one. */
<a name="line135"></a>  TEST_STAGE_ONE_START: 3,
<a name="line136"></a>
<a name="line137"></a>  /** Event indicating the start of test stage two. */
<a name="line138"></a>  TEST_STAGE_TWO_START: 4,
<a name="line139"></a>
<a name="line140"></a>  /** Event indicating the first piece of test data was received. */
<a name="line141"></a>  TEST_STAGE_TWO_DATA_ONE: 5,
<a name="line142"></a>
<a name="line143"></a>  /**
<a name="line144"></a>   * Event indicating that the second piece of test data was received and it was
<a name="line145"></a>   * recieved separately from the first.
<a name="line146"></a>   */
<a name="line147"></a>  TEST_STAGE_TWO_DATA_TWO: 6,
<a name="line148"></a>
<a name="line149"></a>  /** Event indicating both pieces of test data were received simultaneously. */
<a name="line150"></a>  TEST_STAGE_TWO_DATA_BOTH: 7,
<a name="line151"></a>
<a name="line152"></a>  /** Event indicating stage one of the test request failed. */
<a name="line153"></a>  TEST_STAGE_ONE_FAILED: 8,
<a name="line154"></a>
<a name="line155"></a>  /** Event indicating stage two of the test request failed. */
<a name="line156"></a>  TEST_STAGE_TWO_FAILED: 9,
<a name="line157"></a>
<a name="line158"></a>  /**
<a name="line159"></a>   * Event indicating that a buffering proxy is likely between the client and
<a name="line160"></a>   * the server.
<a name="line161"></a>   */
<a name="line162"></a>  PROXY: 10,
<a name="line163"></a>
<a name="line164"></a>  /**
<a name="line165"></a>   * Event indicating that no buffering proxy is likely between the client and
<a name="line166"></a>   * the server.
<a name="line167"></a>   */
<a name="line168"></a>  NOPROXY: 11,
<a name="line169"></a>
<a name="line170"></a>  /** Event indicating an unknown SID error. */
<a name="line171"></a>  REQUEST_UNKNOWN_SESSION_ID: 12,
<a name="line172"></a>
<a name="line173"></a>  /** Event indicating a bad status code was received. */
<a name="line174"></a>  REQUEST_BAD_STATUS: 13,
<a name="line175"></a>
<a name="line176"></a>  /** Event indicating incomplete data was received */
<a name="line177"></a>  REQUEST_INCOMPLETE_DATA: 14,
<a name="line178"></a>
<a name="line179"></a>  /** Event indicating bad data was received */
<a name="line180"></a>  REQUEST_BAD_DATA: 15,
<a name="line181"></a>
<a name="line182"></a>  /** Event indicating no data was received when data was expected. */
<a name="line183"></a>  REQUEST_NO_DATA: 16,
<a name="line184"></a>
<a name="line185"></a>  /** Event indicating a request timeout. */
<a name="line186"></a>  REQUEST_TIMEOUT: 17,
<a name="line187"></a>
<a name="line188"></a>  /**
<a name="line189"></a>   * Event indicating that the server never received our hanging GET and so it
<a name="line190"></a>   * is being retried.
<a name="line191"></a>   */
<a name="line192"></a>  BACKCHANNEL_MISSING: 18,
<a name="line193"></a>
<a name="line194"></a>  /**
<a name="line195"></a>   * Event indicating that we have determined that our hanging GET is not
<a name="line196"></a>   * receiving data when it should be. Thus it is dead dead and will be retried.
<a name="line197"></a>   */
<a name="line198"></a>  BACKCHANNEL_DEAD: 19,
<a name="line199"></a>
<a name="line200"></a>  /**
<a name="line201"></a>   * The browser declared itself offline during the lifetime of a request, or
<a name="line202"></a>   * was offline when a request was initially made.
<a name="line203"></a>   */
<a name="line204"></a>  BROWSER_OFFLINE: 20,
<a name="line205"></a>
<a name="line206"></a>  /** ActiveX is blocked by the machine&#39;s admin settings. */
<a name="line207"></a>  ACTIVE_X_BLOCKED: 21
<a name="line208"></a>};
<a name="line209"></a>
<a name="line210"></a>
<a name="line211"></a>
<a name="line212"></a>/**
<a name="line213"></a> * Event class for STAT_EVENT.
<a name="line214"></a> *
<a name="line215"></a> * @param {goog.events.EventTarget} eventTarget The stat event target for
<a name="line216"></a>       the channel.
<a name="line217"></a> * @param {requestStats.Stat} stat The stat.
<a name="line218"></a> * @constructor
<a name="line219"></a> * @extends {goog.events.Event}
<a name="line220"></a> */
<a name="line221"></a>requestStats.StatEvent = function(eventTarget, stat) {
<a name="line222"></a>  goog.events.Event.call(this, requestStats.Event.STAT_EVENT, eventTarget);
<a name="line223"></a>
<a name="line224"></a>  /**
<a name="line225"></a>   * The stat
<a name="line226"></a>   * @type {requestStats.Stat}
<a name="line227"></a>   */
<a name="line228"></a>  this.stat = stat;
<a name="line229"></a>
<a name="line230"></a>};
<a name="line231"></a>goog.inherits(requestStats.StatEvent, goog.events.Event);
<a name="line232"></a>
<a name="line233"></a>
<a name="line234"></a>/**
<a name="line235"></a> * Returns the singleton event target for stat events.
<a name="line236"></a> * @return {goog.events.EventTarget} The event target for stat events.
<a name="line237"></a> */
<a name="line238"></a>requestStats.getStatEventTarget = function() {
<a name="line239"></a>  return requestStats.statEventTarget_;
<a name="line240"></a>};
<a name="line241"></a>
<a name="line242"></a>
<a name="line243"></a>/**
<a name="line244"></a> * Helper function to call the stat event callback.
<a name="line245"></a> * @param {requestStats.Stat} stat The stat.
<a name="line246"></a> */
<a name="line247"></a>requestStats.notifyStatEvent = function(stat) {
<a name="line248"></a>  var target = requestStats.statEventTarget_;
<a name="line249"></a>  target.dispatchEvent(new requestStats.StatEvent(target, stat));
<a name="line250"></a>};
<a name="line251"></a>
<a name="line252"></a>
<a name="line253"></a>/**
<a name="line254"></a> * An event that fires when POST requests complete successfully, indicating
<a name="line255"></a> * the size of the POST and the round trip time.
<a name="line256"></a> */
<a name="line257"></a>requestStats.Event.TIMING_EVENT = &#39;timingevent&#39;;
<a name="line258"></a>
<a name="line259"></a>
<a name="line260"></a>
<a name="line261"></a>/**
<a name="line262"></a> * Event class for requestStats.Event.TIMING_EVENT
<a name="line263"></a> *
<a name="line264"></a> * @param {goog.events.EventTarget} target The stat event target for
<a name="line265"></a>       the channel.
<a name="line266"></a> * @param {number} size The number of characters in the POST data.
<a name="line267"></a> * @param {number} rtt The total round trip time from POST to response in MS.
<a name="line268"></a> * @param {number} retries The number of times the POST had to be retried.
<a name="line269"></a> * @constructor
<a name="line270"></a> * @extends {goog.events.Event}
<a name="line271"></a> */
<a name="line272"></a>requestStats.TimingEvent = function(target, size, rtt, retries) {
<a name="line273"></a>  goog.events.Event.call(this,
<a name="line274"></a>      requestStats.Event.TIMING_EVENT, target);
<a name="line275"></a>
<a name="line276"></a>  /**
<a name="line277"></a>   * @type {number}
<a name="line278"></a>   */
<a name="line279"></a>  this.size = size;
<a name="line280"></a>
<a name="line281"></a>  /**
<a name="line282"></a>   * @type {number}
<a name="line283"></a>   */
<a name="line284"></a>  this.rtt = rtt;
<a name="line285"></a>
<a name="line286"></a>  /**
<a name="line287"></a>   * @type {number}
<a name="line288"></a>   */
<a name="line289"></a>  this.retries = retries;
<a name="line290"></a>
<a name="line291"></a>};
<a name="line292"></a>goog.inherits(requestStats.TimingEvent, goog.events.Event);
<a name="line293"></a>
<a name="line294"></a>
<a name="line295"></a>/**
<a name="line296"></a> * Helper function to notify listeners about POST request performance.
<a name="line297"></a> *
<a name="line298"></a> * @param {number} size Number of characters in the POST data.
<a name="line299"></a> * @param {number} rtt The amount of time from POST start to response.
<a name="line300"></a> * @param {number} retries The number of times the POST had to be retried.
<a name="line301"></a> */
<a name="line302"></a>requestStats.notifyTimingEvent = function(size, rtt, retries) {
<a name="line303"></a>  var target = requestStats.statEventTarget_;
<a name="line304"></a>  target.dispatchEvent(
<a name="line305"></a>      new requestStats.TimingEvent(
<a name="line306"></a>          target, size, rtt, retries));
<a name="line307"></a>};
<a name="line308"></a>
<a name="line309"></a>
<a name="line310"></a>/**
<a name="line311"></a> * Allows the application to set an execution hooks for when a channel
<a name="line312"></a> * starts processing requests. This is useful to track timing or logging
<a name="line313"></a> * special information. The function takes no parameters and return void.
<a name="line314"></a> * @param {Function} startHook  The function for the start hook.
<a name="line315"></a> */
<a name="line316"></a>requestStats.setStartThreadExecutionHook = function(startHook) {
<a name="line317"></a>  requestStats.startExecutionHook_ = startHook;
<a name="line318"></a>};
<a name="line319"></a>
<a name="line320"></a>
<a name="line321"></a>/**
<a name="line322"></a> * Allows the application to set an execution hooks for when a channel
<a name="line323"></a> * stops processing requests. This is useful to track timing or logging
<a name="line324"></a> * special information. The function takes no parameters and return void.
<a name="line325"></a> * @param {Function} endHook  The function for the end hook.
<a name="line326"></a> */
<a name="line327"></a>requestStats.setEndThreadExecutionHook = function(endHook) {
<a name="line328"></a>  requestStats.endExecutionHook_ = endHook;
<a name="line329"></a>};
<a name="line330"></a>
<a name="line331"></a>
<a name="line332"></a>/**
<a name="line333"></a> * Application provided execution hook for the start hook.
<a name="line334"></a> *
<a name="line335"></a> * @type {Function}
<a name="line336"></a> * @private
<a name="line337"></a> */
<a name="line338"></a>requestStats.startExecutionHook_ = function() { };
<a name="line339"></a>
<a name="line340"></a>
<a name="line341"></a>/**
<a name="line342"></a> * Application provided execution hook for the end hook.
<a name="line343"></a> *
<a name="line344"></a> * @type {Function}
<a name="line345"></a> * @private
<a name="line346"></a> */
<a name="line347"></a>requestStats.endExecutionHook_ = function() { };
<a name="line348"></a>
<a name="line349"></a>
<a name="line350"></a>/**
<a name="line351"></a> * Helper function to call the start hook
<a name="line352"></a> */
<a name="line353"></a>requestStats.onStartExecution = function() {
<a name="line354"></a>  requestStats.startExecutionHook_();
<a name="line355"></a>};
<a name="line356"></a>
<a name="line357"></a>
<a name="line358"></a>/**
<a name="line359"></a> * Helper function to call the end hook
<a name="line360"></a> */
<a name="line361"></a>requestStats.onEndExecution = function() {
<a name="line362"></a>  requestStats.endExecutionHook_();
<a name="line363"></a>};
<a name="line364"></a>
<a name="line365"></a>
<a name="line366"></a>/**
<a name="line367"></a> * Wrapper around SafeTimeout which calls the start and end execution hooks
<a name="line368"></a> * with a try...finally block.
<a name="line369"></a> * @param {Function} fn The callback function.
<a name="line370"></a> * @param {number} ms The time in MS for the timer.
<a name="line371"></a> * @return {number} The ID of the timer.
<a name="line372"></a> */
<a name="line373"></a>requestStats.setTimeout = function(fn, ms) {
<a name="line374"></a>  if (!goog.isFunction(fn)) {
<a name="line375"></a>    throw Error(&#39;Fn must not be null and must be a function&#39;);
<a name="line376"></a>  }
<a name="line377"></a>  return goog.global.setTimeout(function() {
<a name="line378"></a>    requestStats.onStartExecution();
<a name="line379"></a>    try {
<a name="line380"></a>      fn();
<a name="line381"></a>    } finally {
<a name="line382"></a>      requestStats.onEndExecution();
<a name="line383"></a>    }
<a name="line384"></a>  }, ms);
<a name="line385"></a>};
<a name="line386"></a>});  // goog.scope
</pre>


</body>
</html>
